AOSP下载和编译

###windows下的源码下载

####创建目录
新建目录 aosp,cd到目录下
然后 通过 git克隆以下镜像,这会在目录下创建manifest目录
git clone https://android.googlesource.com/platform/manifest.git
//没有梯子使用清华源
git clone https://aosp.tuna.tsinghua.edu.cn/platform/manifest.git

cd到manifest目录中,通过git branch -a查看分支版本
找到想要下载的分支版本进行下载,如
git checkout android-6.0.1_r79

最后通过以下python脚本进行下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import xml.dom.minidom
import os
from subprocess import call

# 1. 修改为源码要保存的路径
rootdir = "D:/android_source_code/Android_6_0_1"

# 2. 设置 git 安装的路径
git = "C:/Develop/Git/bin/git.exe"

# 3. 修改为第一步中 manifest 中 default.xml 保存的路径
dom = xml.dom.minidom.parse("D:/android_source_code/manifest/default.xml")
root = dom.documentElement

#prefix = git + " clone https://android.googlesource.com/"
# 4. 没有梯子使用清华源下载
prefix = git + " clone https://aosp.tuna.tsinghua.edu.cn/"
suffix = ".git"

if not os.path.exists(rootdir):
os.mkdir(rootdir)

for node in root.getElementsByTagName("project"):
os.chdir(rootdir)
d = node.getAttribute("path")
last = d.rfind("/")
if last != -1:
d = rootdir + "/" + d[:last]
if not os.path.exists(d):
os.makedirs(d)
os.chdir(d)
cmd = prefix + node.getAttribute("name") + suffix
call(cmd)

###Android源码的下载

####下载repo并配置repo

1
2
3
4
5
6
7
8
sudo apt-get install git-core curl
git config --global user.email '邮箱'
git config --global user.name '名字'

mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

####创建源码路径

1
2
mkdir aosp
cd aosp

####下载

执行init

repo init -u https://android.googlesource.com/platform/manifest

这样下载的是master的代码,如果要指定某个版本,可以通过

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-4.4.4_r1

如果提示无法连接到 gerrit.googlesource.com,可以编辑 ~/bin/repo,把 REPO_URL 一行替换成
REPO_URL = ‘https://mirrors.tuna.tsinghua.edu.cn/git/git-repo

然后下载通过

repo sync

####JDK的配置

编译KK(android4.4)需要jdk的版本是1.6,注意这里的jdk为oracle,不能是openjdk,

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo mkdir /usr/local/jdk #jdk的安装路径
sudo cp /home/youlor/Downloads/jdk-6u45-linux-x64.bin /usr/local/jdk
#其中/home/youlor/Downloads/jdk-6u45-linux-x64.bin为jdk1.60_45的二进制文件所在路径
cd /usr/local/jdk
sudo chmod +x jdk-6u45-linux-x64.bin
sudo ./jdk-6u45-linux-x64.bin

#安装成功后,配置环境变量
sudo gedit /etc/profile #打开配置文件将下面4行加入到末尾并保存退出
export JAVA_HOME=/usr/local/jdk/jdk1.6.0_45
export JRE_HOME=/usr/local/jdk/jdk1.6.0_45/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$JAVA_HOME:$PATH

make降级

在ubuntu16.04上的make版本默认是4.1,但编译kk需要的版本为3.81或者3.82,所以需要下载安装make-3.82

1
2
3
4
5
6
tar -xjvf make-3.81.tar.bz2
./configure
make
sudo make install

最后通过make -v查看,安装完成后需要重新登录。

####编译源码

1
2
3
source build/envsetup.sh
luanch aosp_arm64-eng
make -j4

###下载及编译kernel

####kernel下载

1
2
3
4
5
lisc@lisc-ubuntu:/home/aosp/android-4.4$ mkdir kernel
lisc@lisc-ubuntu:/home/aosp/android-4.4$ cd kernel
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel$ git clone https://aosp.tuna.tsinghua.edu.cn/kernel/goldfish.git
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel$ git branch -a
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ git checkout -b android-goldfish-3.4 remotes/origin/android-goldfish-3.4

####编译环境的配置

1
2
3
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ export ARCH=arm
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ export SUBARCH=arm
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ export CROSS_COMPILE=arm-eabi-

将交叉编译工具设置到PATH中

lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ export PATH=$PATH:/home/aosp/android-4.4/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin

####编译kernel

1
2
3
4
5
6
7
8
9
10
11
12
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ make goldfish_armv7_defconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
#
# configuration written to .config
#
lisc@lisc-ubuntu:/home/aosp/android-4.4/kernel/goldfish$ make
坚持原创技术分享,您的支持将鼓励我继续创作!